const app = getApp()
var util = require('../../utils/util.js');
var status = require('../../utils/index.js');

Page({
    mixins: [require('../../mixin/globalMixin.js')],
    data: {
        loadMore: true,
        tabTop: 0,
        showSubCate: true,
        shop_info: {},
        car_count: 0,
        rushList: [], //店铺列表
        pageNum: 1,
        loadMore: true,
        showEmpty: false,
    },

    onLoad: function() {
        this.getInfo();
        this.getData();
        this.setData({
            navH: app.globalData.navHeight
        })
    },

    onReady: function() {},

    onShow: function() {
        const that = this;
        util.check_login_new().then((res) => {
            let needAuth = !res;
            that.setData({
                needAuth
            })
            if (res) {
                (0, status.cartNum)('', true).then((res) => {
                    res.code == 0 && that.setData({
                        cartNum: res.data
                    })
                });
            }
        })
    },

    onPageScroll: function(event) {
        if (event.scrollTop > this.data.tabTop) {
            if (this.data.tabFix) {
                return
            } else {
                this.setData({
                    oneFixed: "Fixed"
                })
            }
        } else {
            this.setData({
                oneFixed: ''
            })
        }
    },

    noLogin: function() {
        this.setData({
            needAuth: true,
            showAuthModal: true
        })
    },

    /**
     * 授权成功回调
     */
    authSuccess: function() {
        const that = this;
        this.data.pageNum = 1;
        this.setData({
            loadMore: true,
            showEmpty: false,
            needAuth: false,
            showAuthModal: false
        }, () => {
            that.getData()
        })
    },

    authModal: function() {
        if (this.data.needAuth) {
            this.setData({
                showAuthModal: !this.data.showAuthModal
            });
            return false;
        }
        return true;
    },

    onPullDownRefresh: function() {
        const that = this;
        this.data.pageNum = 1;
        this.setData({
            loadMore: true,
            showEmpty: false
        }, () => {
            that.getInfo();
            that.getData()
        })
    },

    onReachBottom: function() {
        var that = this;
        if (that.data.loadMore) {
            var pageNum = that.data.pageNum
            pageNum++;
            that.setData({
                pageNum: pageNum
            })
            this.getData();
        }
    },

    /**
     * 获取列表
     */
    getData: function(keyword = '') {
        return new Promise((resolve, reject) => {
            let that = this;
            let token = wx.getStorageSync('token');
            let gid = that.data.classificationId;

            // wx.showLoading();
            app.util.request({
                'url': 'entry/wxapp/index',
                'data': {
                    controller: 'recipe.get_recipe_list_other',
                    token: token,
                    per_page: 8,
                    pageNum: that.data.pageNum,
                    keyword
                },
                dataType: 'json',
                success: function(res) {
                    wx.stopPullDownRefresh();
                    if (res.data.code == 1) {
                        let list = res.data.data;
                        if (res.data.data) {
                            var rushList = that.data.rushList.concat(res.data.data);
                            that.setData({
                                rushList: rushList
                            })
                        }
                        if (that.data.pageNum == 1 && that.data.rushList.length == 0) {
                            that.setData({
                                showEmpty: true
                            })
                        }
                    }
                    // wx.hideLoading();
                }
            })
        })
    },

    /**
     * 获取信息
     */
    getInfo: function() {
        let that = this;
        let token = wx.getStorageSync('token');

        // wx.showLoading();
        app.util.request({
            'url': 'entry/wxapp/index',
            'data': {
                controller: 'recipe.get_index_info_other'
            },
            dataType: 'json',
            success: function(res) {
                if (res.data.code) {
                    let {
                        is_open_recipe,
                        modify_recipe_name,
                        modify_recipe_share_title,
                        modify_vipcard_share_image,
                        index_youshi_backgroundimage_bottom,
                        index_youshi_backgroundimage_top
                    } = res.data.code;
                    if (is_open_recipe != 1) {
                        app.util.message('未开启功能', 'switchTo:/lionfish_comshop/pages/index/index', 'error');
                        return;
                    }

                    wx.setNavigationBarTitle({
                        title: modify_recipe_name || '菜谱'
                    })

                    // 菜单
                    let params = {
                        classification: {
                            activeIndex: 0
                        }
                    };
                    let index_type_first_name = '全部';
                    that.setData({
                        modify_recipe_share_title,
                        modify_vipcard_share_image,
                        modify_recipe_name: modify_recipe_name || '菜谱',
                        index_youshi_backgroundimage_bottom,
                        index_youshi_backgroundimage_top,
                        ...params
                    })
                }
                // wx.hideLoading();
            }
        })
    },

    goLink: function(event) {
        if (!this.authModal()) return;
        let link = event.currentTarget.dataset.link;
        var pages_all = getCurrentPages();
        if (pages_all.length > 3) {
            wx.redirectTo({
                url: link
            })
        } else {
            wx.navigateTo({
                url: link
            })
        }
    },
    playVideo: function(e) {
        console.log(e);
        var id = e.currentTarget.dataset.id,
            index = e.currentTarget.dataset.index,
            that = this,
            token = wx.getStorageSync('token'),
            spuItem = e.currentTarget.dataset.item,
            rushList = that.data.rushList;
        app.util.request({
            'url': 'entry/wxapp/index',
            'data': {
                controller: 'recipe.play_recipe_do',
                token: token,
                id: spuItem.id
            },
            dataType: 'json',
            success: function(res) {
                if (res.data.code == 0) {
                    if (that.data.indexCurrent ==undefined) {
                        var up = "rushList[" + index + "].showVideo";
                        that.setData({
                            indexCurrent: index,
                            [up]: false
                        })
                        that.videoContext = wx.createVideoContext(id)
                        that.videoContext.play()
                    }else{
                        console.log(that.data.indexCurrent);
                        var videoContextPrev = wx.createVideoContext('myVideo' + that.data.indexCurrent)
                        if (that.data.indexCurrent != index) {
                            videoContextPrev.pause()
                        }
                        that.setData({
                            indexCurrent: index
                        })
                        for(var i in rushList){
                            var up = "rushList[" + i + "].showVideo", playnum = "rushList[" + i + "].playnum";
                            that.setData({
                                [up]: true,
                                [playnum]: res.data.playnum
                            })
                        }
                        var upNow = "rushList[" + index + "].showVideo";
                        that.setData({
                            indexCurrent: index,
                            [upNow]: false
                        })
                        that.videoContext = wx.createVideoContext(id);
                        that.videoContext.play();
                    }
                    

                } else if (res.data.code == 1) {
                    //未登录
                    that.authModal();
                }
            }
        })
    },
    videEndPlay: function(e) {
        console.log(e);
        var index = e.currentTarget.dataset.index;
        var upNow = "rushList[" + index + "].showVideo";
        this.setData({
            [upNow]: true
        })
    },
    goDetail: function(e) {
        let idx = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/lionfish_comshop/moduleA/menu/details?&id=' + idx,
        })
    },
    onShareAppMessage: function() {
        return {
            path: "lionfish_comshop/moduleA/menu/other",
            success: function() {},
            fail: function() {}
        };
    },
    agree: function(e) {
        var id = e.currentTarget.dataset.id,
            index = e.currentTarget.dataset.index,
            that = this,
            token = wx.getStorageSync('token'),
            spuItem = e.currentTarget.dataset.item,
            rushList = that.data.rushList;

        app.util.request({
            'url': 'entry/wxapp/index',
            'data': {
                controller: 'recipe.fav_recipe_do',
                token: token,
                id: spuItem.id
            },
            dataType: 'json',
            success: function(res) {
                if (res.data.code == 0) {
                    //成功
                    // wx.showToast({
                    //     title: '已喜欢~',
                    //     icon: 'none'
                    // })
                    var fav_count = "rushList[" + index + "].fav_count", has_fav = "rushList[" + index + "].has_fav";
                    that.setData({
                        [fav_count]: res.data.fav_count,
                        [has_fav]: 1
                    })
                } else if (res.data.code == 1) {
                    //未登录
                    that.authModal();
                } else if (res.data.code == 2) {
                    //取消收藏
                    var fav_count = "rushList[" + index + "].fav_count", has_fav = "rushList[" + index + "].has_fav";
                    that.setData({
                        [fav_count]: res.data.fav_count,
                        [has_fav]: 0
                    })
                    // wx.showToast({
                    //     title: '取消喜欢~',
                    //     icon: 'none'
                    // })
                }
            }
        })
    },
    stopOtherPlay: function() {
        for (var i in that.data.spuItemList) {
            var videoContext = wx.createVideoContext('myVideo' + that.data.spuItemList.id);
            videoContext.pause();
        }
    }

})